import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateOrEditMeasurementUnitModalComponent } from './create-or-edit-measurement-modal.component';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent, MenuItem } from 'primeng/primeng';
import {
    Base_MeasurementUnitServiceProxy,
    UserDataPermissionServiceProxy,
    Base_MeasurementUnitListDto,
    GetBase_MeasurementUnitInput, 
    ListDtoExtensionsItem
} from '@shared/service-proxies/stock-service-proxies';
import * as moment from 'moment';
@Component({
    templateUrl: './measurement.component.html',
    animations: [appModuleAnimation()]
})
export class MeasurementUnitComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditMeasurementUnitModal')
    createOrEditMeasurementUnitModal: CreateOrEditMeasurementUnitModalComponent;
    extensions: Array<ListDtoExtensionsItem>;
    anyncing = false;
    filterText = '';
    f_BillDate: Date;
    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];
    userDataPermissions;
    selectedDepts;
    listId = new Array<number>();
    selectedValues = new Base_MeasurementUnitListDto();
    itemContextMenuItems: MenuItem[];
    constructor(injector: Injector, private _base_MeasurementUnitServiceProxy: Base_MeasurementUnitServiceProxy) {
        super(injector);
    }

    ngOnInit() {}

    getTableData(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        this.selectedValues = new Base_MeasurementUnitListDto();
        this.primengTableHelper.showLoadingIndicator();
        this._base_MeasurementUnitServiceProxy
            .getMeasurementUnitListExtention(
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.base_MeasurementUnitListDto.totalCount;
                this.primengTableHelper.records = result.base_MeasurementUnitListDto.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    delete(id: number) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._base_MeasurementUnitServiceProxy.deleteMeasurementUnit(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getTableData();
                });
            }
        });
    }

    // selectedDelete() {
    //     this.message.confirm(this.l('AreYouSure'), isConfirmed => {
    //         if (isConfirmed) {
    //             this.selectedValues.forEach(element => {
    //                 this.listId.push(element.id);
    //             });
    //             this._base_MeasurementUnitServiceProxy.deleteSelectedMeasurementUnit(this.listId).subscribe(() => {
    //                 this.notify.success(this.l('SuccessfullyDeleted'));
    //                 this.getTableData();
    //             });
    //         }
    //     });
    // }

    batchDelete() {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                let input = new GetBase_MeasurementUnitInput();

                this._base_MeasurementUnitServiceProxy.batchDeleteMeasurementUnit(input).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getTableData();
                });
            }
        });
    }
}
